using System;
using System.Collections.Generic;
using System.Linq;
using DotNetNuke.Data;
using DotNetNuke.Entities.Users;

namespace YourCompany.DNNModules.DNNImageGallery
{

    /// ----------------------------------------------------------------------------- 
    /// <summary> 
    /// The Controller class for DNNImageGallery
    /// </summary> 
    /// <remarks> 
    /// </remarks> 
    /// <history> 
    /// </history> 
    /// ----------------------------------------------------------------------------- 

    // [DNNtc.UpgradeEventMessage("01.01.01,04.00.02,04.01.00")]
    [DNNtc.BusinessControllerClass()]
    public class DNNImageGalleryController // : ISearchable, IPortable
    {
        private int UserID
        {
            get { return UserController.GetCurrentUserInfo().UserID; }
        }

        #region "Public Methods"
        public ItemInfo InsertItem(ItemInfo item)
        {
            using (IDataContext context = DataContext.Instance())
            {
                var repository = context.GetRepository<ItemInfo>();
                item.CreatedOnDate = DateTime.Now;
                item.CreatedByUserID = UserID;
                item.LastModifiedByUserID = UserID;
                item.LastModifiedOnDate = DateTime.Now;
                repository.Insert(item);
                return item;
            }
        }
        public ItemInfo UpdateItem(ItemInfo item)
        {
            using (IDataContext context = DataContext.Instance())
            {
                var repository = context.GetRepository<ItemInfo>();
                item.LastModifiedByUserID = UserID;
                item.LastModifiedOnDate = DateTime.Now;
                repository.Update(item);
                return item;
            }
        }
        public void DeleteItem(ItemInfo item)
        {
            using (IDataContext context = DataContext.Instance())
            {
                var repository = context.GetRepository<ItemInfo>();
                repository.Delete(item);
            }
        }

        public IEnumerable<ItemInfo> GetAllItems(int moduleId)
        {
            IEnumerable<ItemInfo> items;
            using (IDataContext context = DataContext.Instance())
            {
                var repository = context.GetRepository<ItemInfo>();
                items = repository.Find("WHERE ModuleId = @0", moduleId);
            }
            return items;
        }

        #endregion

        //#region Implementation of ISearchable

        ///// ----------------------------------------------------------------------------- 
        ///// <summary> 
        ///// GetSearchItems implements the ISearchable Interface 
        ///// </summary> 
        ///// <remarks> 
        ///// </remarks> 
        ///// <param name="module">The ModuleInfo for the module to be Indexed</param> 
        ///// <history> 
        ///// </history> 
        ///// ----------------------------------------------------------------------------- 
        //public DotNetNuke.Services.Search.SearchItemInfoCollection GetSearchItems(ModuleInfo module)
        //{ 
        //    
        //    SearchItemInfoCollection SearchItemCollection = new SearchItemInfoCollection(); 
        //    
        //    IEnumerable<ItemInfo> items = GetAllItems(module.ModuleID); 
        //    foreach ( ItemInfo item in items) 
        //    { 
        //        SearchItemInfo searchItem = new SearchItemInfo(module.ModuleTitle, 
        //				item.Title,
        //				item.CreatedByUserID ?? -1,
        //				item.CreatedOnDate, 
        //				module.ModuleID, 
        //				item.ItemId.ToString(), 
        //				item.Content, 
        //				"ItemId=" + item.ItemId.ToString()); 
        //        SearchItemCollection.Add(searchItem); 
        //    } 
        //    return SearchItemCollection; 
        //    
        //}

        //#endregion

        //#region Implementation of IPortable

        ///// ----------------------------------------------------------------------------- 
        ///// <summary> 
        ///// ExportModule implements the IPortable ExportModule Interface 
        ///// </summary> 
        ///// <remarks> 
        ///// </remarks> 
        ///// <param name="moduleID">The Id of the module to be exported</param> 
        ///// <history> 
        ///// </history> 
        ///// ----------------------------------------------------------------------------- 
        //public string ExportModule(int moduleID) 
        //{ 
        //    IEnumerable<ItemInfo> items = GetAllItems(moduleID); 
        //    string strXML = "<DNNImageGallery_Items>"; 
        //    foreach ( ItemInfo item in items) 
        //    { 
        //        strXML += "<DNNImageGallery_Item>"; 
        //        strXML += "<Title>" +XmlUtils.XMLEncode(item.Title) + "</Title>"; 
        //        strXML += "<Content>" +XmlUtils.XMLEncode(item.Content) + "</Content>";
        //        strXML += "<CreatedByUserID>" +XmlUtils.XMLEncode(item.CreatedByUserID.ToString()) + "</CreatedByUserID>";
        //        strXML += "<CreatedOnDate>" + XmlUtils.XMLEncode(item.CreatedOnDate.ToString()) + "</CreatedOnDate>";
        //        strXML += "</DNNImageGallery_Item>"; 
        //    } 
        //    strXML += "</DNNImageGallery_Items>"; 
        //    return strXML; 
        //}

        ///// ----------------------------------------------------------------------------- 
        ///// <summary> 
        ///// ImportModule implements the IPortable ImportModule Interface 
        ///// </summary> 
        ///// <remarks> 
        ///// </remarks> 
        ///// <param name="moduleID">The Id of the module to be imported</param> 
        ///// <param name="name">The name to be imported</param> 
        ///// <param name="version">The version of the module to be imported</param> 
        ///// <param name="userId">The Id of the user performing the import</param> 
        ///// <history> 
        ///// </history> 
        ///// ----------------------------------------------------------------------------- 
        //public void ImportModule(int moduleID, string name, string version, int userId) 
        //{ 
        //    
        //    XmlNode xmlItems = Globals.GetContent(name, "DNNImageGallery_Items"); 
        //    foreach ( XmlNode xmlItem in xmlItems.SelectNodes("DNNImageGallery_Item")) 
        //    { 
        //        ItemInfo item = new ItemInfo(); 
        //        item.ModuleId = moduleID; 
        //        item.Title = xmlItem.SelectSingleNode("Title").InnerText; 
        //        item.Company = xmlItem.SelectSingleNode("Company").InnerText;//        
        //        item.CreatedByUserID = Int32.Parse(xmlItem.SelectSingleNode("CreatedByUserID").InnerText);
        //        item.CreatedOnDate = DateTime.Parse(xmlItem.SelectSingleNode("CreatedOnDate").InnerText);
        //        InsertItem(item); 
        //    } 
        //}

        //#endregion

    }
}